jsx3.require("jsx3.gui.Template");

/**
 * Renders an IFrame.
 *
 * @since 3.4
 */
jsx3.Class.defineClass("ux.core.gui.v11.IFrame", jsx3.gui.Template.Block, null, function(IFrame, IFrame_prototype){

    /**
     * {int}
     * @final @jsxobf-final
     */
    IFrame.SCROLL_YES = 'yes';
    /**
     * {int}
     * @final @jsxobf-final
     */
    IFrame.SCROLL_NO = 'no';
    /**
     * {int}
     * @final @jsxobf-final
     */
    IFrame.SCROLL_AUTO = 'auto';
    /**
     * {int}
     * @final @jsxobf-final
     */
    IFrame.FRAMEBORDER_YES = 1;
    
    /**
     * {int}
     * @final @jsxobf-final
     */
    IFrame.FRAMEBORDER_NO = 0;
    
    /**
     * this also be properties.
     * {Object}
     * @final @jsxobj-final
     */
    IFrame.TRIGGERS = {
        TITLE: 'title',
        LOCATION: 'src',
        FRAMEBORDER: 'frameborder',
        SCROLLING: 'scrolling'
    };
    
    IFrame_prototype.init = function(strName){
        jsx3.log('loading iframe...')
        this.jsxsuper(strName);
    };
    
    IFrame_prototype.onAfterPaint = function(objParent){
        jsx3.log('onAfterPaint...');
		this.setSrc(this.jsxsrc); //after paint then resolve the url.
    };
    
    IFrame_prototype.onAfterAttach = function(objParent){
        jsx3.log('onAfterAttach...');
		jsx3.log('...getUriResolver() : ' + this.getUriResolver());
    };
	
    IFrame_prototype.onSetParent = function(objParent){
        //you can do something here when drop the component.
        jsx3.log('onSetParent...');
        
        if (this.src && this.src != 'about:blank') {
            jsx3.log('...getUriResolver() : ' + this.getUriResolver());
            jsx3.log('this.src : ' + this.src);
            jsx3.log('this.jsxsrc : ' + this.jsxsrc);
            this.src = this.jsxsrc; //to set a oraginal path for GI Builder to display the path. because the Properties Editor not display right.
        }
        else this.src = 'about:blank';
        
        if (!this.frameborder) this.frameborder = 0;
        
        if (!this.scrolling) this.scrolling = 'auto';
        
        return true;
    };
    
    IFrame_prototype.repaint = function(){
        jsx3.log('repaint iframe.');
        return this.jsxsuper();
    };
    
    IFrame_prototype.onload = function(objEvent, objGUI){
        this.doEvent(jsx3.gui.Interactive.JSXLOAD, {
            objEVENT: objEvent
        });
    };
    
    IFrame_prototype.getTitle = function(){
        return this.title;
    };
    
    IFrame_prototype.setTitle = function(strTitle, bRepaint){
        this.setProperty(IFrame.TRIGGERS.TITLE, strTitle);
        if (bRepaint) {
            this.repaint();
        }
        return this;
    };
    
    /**
     * Returns the URI of this iframe.
     * @return {String}
     */
    IFrame_prototype.getSrc = function(){
        jsx3.log('getSrc ... ');
        return this.jsxsrc;
    };
    
    /**
     * Sets the URI of this iframe. The URI can be absolute or relative from the content base of the server that
     * owns this object. If this iframe is rendered on screen, its location is updated immediately.
     * @param srcSrc {String}
     * @return {ux.core.gui.IFrame} this object.
     */
    IFrame_prototype.setSrc = function(srcSrc, bRepaint){
        this.jsxsrc = srcSrc;
        if (srcSrc != null && srcSrc != "") {
            srcSrc = this.getUriResolver().resolveURI(srcSrc).toString();
        }
        else {
            srcSrc = 'about:blank';
        }
        
        //if use 'this.src', the url will return a resolved URL. because 'src' is rewrited by TL engine. this may be a bug.
        jsx3.log('this.src = ' + this.src);
        jsx3.log('this.getSrc() = ' + this.getSrc());
        
        this.setProperty(IFrame.TRIGGERS.LOCATION, srcSrc);
        this.src = this.jsxsrc;
        if (bRepaint) {
            this.repaint();
        }
        return this;
    };
    
    /**
     * Returns the scroll mode of this iframe.
     * @return {String}
     */
    IFrame_prototype.getScrolling = function(){
        return this.scrolling;
    };
    
    /**
     * Sets the scroll mode of this iframe.
     * @param strScrolling {String} one of <code>SCROLL_YES</code>, <code>SCROLL_NO</code>, or <code>SCROLL_AUTO</code>.
     * @return {ux.core.gui.IFrame} this object.
     */
    IFrame_prototype.setScrolling = function(strScrolling, bRepaint){
        this.setProperty(IFrame.TRIGGERS.SCROLLING, strScrolling);
        if (bRepaint) {
            this.repaint();
        }
        return this;
    };
    
    /**
     * Returns the frameborder mode of this iframe.
     * @return {int}
     */
    IFrame_prototype.getFrameborder = function(){
        return this.frameborder;
    };
    
    /**
     * Sets the frameborder mode of this iframe.
     * @param intFrameborder {int} one of <code>FRAMEBORDER_YES</code> or <code>FRAMEBORDER_NO</code>.
     * @return {ux.core.gui.IFrame} this object.
     */
    IFrame_prototype.setFrameborder = function(intFrameborder, bRepaint){
        this.setProperty(IFrame.TRIGGERS.FRAMEBORDER, intFrameborder);
        if (bRepaint) {
            this.repaint();
        }
        return this;
    };
    
    //this may be can bind the iframe with CDF.
    IFrame_prototype.setCDFAttribute = function(strAttName){
        //this.setSrc(strAttName);
        jsx3.log('IFrame setCDFAttribute : ' + strAttName);
        this.setProperty("cdfattribute", strAttName);
    };
    
    IFrame_prototype.getCDFAttribute = function(){
        return this.cdfattribute;
    };
    
    // no children allowed
    IFrame_prototype.onSetChild = function(objChild){
        return false;
    };
    
    IFrame.templateXML = ux.core.System.getServer().loadInclude("jsxaddin://user!LiveCore/classes/ux/core/gui/templates/IFrame.xml", "IFrameTemplate_XML", "xml").toString();
    //jsx3.log(IFrame.templateXML);
    jsx3.gui.Template.compile(IFrame.templateXML, IFrame.jsxclass);
    
});
